package com.ai.service.made.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.made.PlcLogs;
import com.ai.mapper.made.PlcLogsMapper;
import com.ai.model.param.PlcLogsParams;
import com.ai.service.made.IPlcLogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * PLC日志记录 服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2023-04-18 10:42:26
 */
@Service
public class PlcLogsServiceImpl extends ServiceImpl<PlcLogsMapper, PlcLogs> implements IPlcLogsService {

    @Override
    public CommonResponse<String> add(PlcLogsParams plcLogsParams) {

        PlcLogs plcLogs = new PlcLogs();
        plcLogs.setFunctionType(Integer.valueOf(plcLogsParams.getFunctionType()));
        plcLogs.setSwitchStatus(Integer.valueOf(plcLogsParams.getSwitchStatus()));
        plcLogs.setMessageContent(plcLogsParams.getMessageContent());

        Date date = new Date();
        plcLogs.setCreateTime(date);
        plcLogs.setUpdateTime(date);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null) {
            String username = authentication.getName();
//            SysUser operatUser = this.selectByName(username);
//            if (null!=operatUser){
//                sysUser.setCreateUser(operatUser.getId());
//                sysUser.setUpdateUser(operatUser.getId());
//            }
//            // 可以根据username查询数据库获取更多用户信息
//            log.info(username);
            plcLogs.setCreateUser(username);
            plcLogs.setUpdateUser(username);
        }

        boolean save = this.save(plcLogs);
        if (save){
            return  CommonResponse.ok("新增成功");
        }else {
            return CommonResponse.error("新增失败");
        }
    }

    /**
     * 更新操作
     * @param plcLogsParams
     * @return
     */
    @Override
    public CommonResponse<String> updatePlcBlog(PlcLogsParams plcLogsParams) {

        PlcLogs plcLogs = new PlcLogs();
        plcLogs.setId(Integer.valueOf(plcLogsParams.getId()));
        plcLogs.setFunctionType(Integer.valueOf(plcLogsParams.getFunctionType()));
        plcLogs.setSwitchStatus(Integer.valueOf(plcLogsParams.getSwitchStatus()));
        plcLogs.setMessageContent(plcLogsParams.getMessageContent());

        Date date = new Date();
        plcLogs.setUpdateTime(date);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null) {
            String username = authentication.getName();
            plcLogs.setUpdateUser(username);
        }

        boolean b = this.updateById(plcLogs);
        if (b){
            return CommonResponse.ok("更新成功");
        }else {
            return CommonResponse.error("更新失败");
        }
    }

    @Override
    public CommonResponse<PlcLogs> queryByLogId(Integer id) {
        if (null == id){
            return CommonResponse.error("日志id不能为空");
        }else {
            PlcLogs plcLogs = this.getById(id);
            return CommonResponse.ok("查询成功",plcLogs);
        }
    }
}
